package es.udima.geo.geoserver.rest;

import java.util.Collection;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import es.udima.geo.geoserver.business.GpsDataController;
import es.udima.geo.geoserver.entities.GpsDataFilter;
import es.udima.geo.geoserver.entities.GpsDataRetrieveResult;
import es.udima.geo.geoserver.entities.GpsDataRetrieveResultError;
import es.udima.geo.geoserver.rest.entities.GpsDataRetrieveResponse;
import es.udima.geo.geoserver.rest.entities.IResponseError;

@Path("/admin/gps")
public class GpsDataAdminService {

    public GpsDataAdminService() {
    }

    /**
     * UserId in filter is a mandatory parameter. If no date filter is set it
     * will return the last gps data of the last day the user sent data. If date
     * filter is for more than MAX_DAYS_TO_RETRIVE_GPS_DATA it will return an
     * error.
     * 
     * @param filter
     * @return
     */
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public GpsDataRetrieveResponse retrieveUserGpsData(GpsDataFilter req) {
	
	GpsDataRetrieveResponse res = new GpsDataRetrieveResponse();

	GpsDataController gc = new GpsDataController();
	GpsDataRetrieveResult gpsRes = gc.retrieveGpsData(req);
	if (gpsRes.isSuccessful()) {
	    res.setFilter(gpsRes.getFilter());
	    res.setGpsData(gpsRes.getGpsData());
	} else {
	    res.addResponseErrors(translateGpsDataRetrieveErrors(gpsRes.getErrors()));
	}

	return res;
    }

    /*-----------------------------------------------------------------------------------------------*/

    /**
     * 
     * @param errors
     * @return
     */
    private Collection<IResponseError> translateGpsDataRetrieveErrors(Collection<GpsDataRetrieveResultError> errors) {
	return new RestServiceErrorHelper().translateGpsDataRetrieveErrors(errors);
    }
}
